//
//  YYEPlayerView.m
//  YuYingEDU
//
//  Created by 胡波 on 2018/1/30.
//  Copyright © 2018年 胡波. All rights reserved.
//

#import "YYEPlayerView.h"

@interface YYEPlayerView ()

@end

@implementation YYEPlayerView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
//        self.backgroundColor = [UIColor blackColor];

        self.player = [[AliyunVodPlayer alloc]init];
        [self.player setAutoPlay:YES];
        
        self.playerView = [[UIView alloc]init];
        self.playerView = self.player.playerView;
        [self.player prepareWithVid:@"1bc3a58267c94cc580b7f11dc99eb09c" accessKeyId:@"LTAI6e1l2xmvIUaK" accessKeySecret:@"FuUEEoP1qz7dfnYY8JmJ76B7K5wyTF" securityToken:@"4qw3eszhhfd77yeyaqawrsee2tqwbhw46rnou2fm9opym4fjq41owjhv779pwfys"];
        [self addSubview:self.playerView];
        
        self.mainView = [[UIView alloc]init];
        self.mainView.backgroundColor = [UIColor whiteColor];
        self.mainView.alpha = 0.7;
        [self addSubview:self.mainView];
        
        self.playBtn = [[UIButton alloc]init];
        [self.playBtn setImage:[UIImage imageNamed:@"player_3"] forState:UIControlStateNormal];
        [self.playBtn setImage:[UIImage imageNamed:@"player_1"] forState:UIControlStateSelected];
        [self.mainView addSubview:self.playBtn];
        
        self.beginLabel = [[UILabel alloc]init];
        self.beginLabel.font = [UIFont systemFontOfSize:13];
        self.beginLabel.textColor = [UIColor whiteColor];
        [self.mainView addSubview:self.beginLabel];
        
        self.playSlider = [[UISlider alloc]init];
        self.playSlider.minimumTrackTintColor = [UIColor yellowColor];
//        self.playSlider.maximumTrackTintColor = [UIColor redColor];
        self.playSlider.value = 0.3;
        [self.mainView addSubview:self.playSlider];
        
        self.loadedProgress = [[UIProgressView alloc]init];
        self.loadedProgress.progressTintColor = [UIColor grayColor];
        self.loadedProgress.progress = 0.5;
        [self.mainView addSubview:self.loadedProgress];
        
        self.endLabel = [[UILabel alloc]init];
        self.endLabel.font = [UIFont systemFontOfSize:13];
        self.endLabel.textColor = [UIColor whiteColor];
        [self.mainView addSubview:self.endLabel];
        
        self.rotationBtn = [[UIButton alloc]init];
        [self.rotationBtn setImage:[UIImage imageNamed:@"player_2"] forState:UIControlStateNormal];
        [self.rotationBtn setImage:[UIImage imageNamed:@"player_2"] forState:UIControlStateSelected];
        [self.rotationBtn addTarget:self action:@selector(rotationClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.mainView addSubview:self.rotationBtn];
        
    }
    return self;
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
//    CGRect bounds = [UIScreen mainScreen].bounds;
//    [self.playerView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.left.equalTo(@(0));
//        make.width.equalTo(@(bounds.size.width));
//        make.height.equalTo(@(bounds.size.height));
//    }];
    
    self.playerView.frame = self.bounds;
    
    [self.mainView makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.height.equalTo(45);
    }];
    
    [self.playBtn makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mainView.left).offset(20);
        make.centerY.equalTo(self.mainView.centerY);
        make.size.equalTo(CGSizeMake(30, 30));
    }];
    
    [self.beginLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.playBtn.right).offset(10);
        make.centerY.equalTo(self.mainView.centerY);
    }];
    
    [self.rotationBtn makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mainView.right).offset(-20);
        make.centerY.equalTo(self.mainView.centerY);
        make.size.equalTo(CGSizeMake(30, 30));
    }];
    
    [self.endLabel makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.rotationBtn.left).offset(-10);
        make.centerY.equalTo(self.mainView.centerY);
    }];
    
    [self.playSlider makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.beginLabel.right).offset(10);
        make.right.equalTo(self.endLabel.left).offset(-10);
        make.centerY.equalTo(self.mainView.centerY);
    }];
    
    [self.loadedProgress makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.beginLabel.right).offset(10);
        make.right.equalTo(self.endLabel.left).offset(-10);
        make.centerY.equalTo(self.mainView.centerY);
    }];
    
}

- (void)rotationClicked:(UIButton *)button {
    
    if ([RotationScreen isOrientationLandscape]) { // 如果是横屏，
        [RotationScreen forceOrientation:(UIInterfaceOrientationPortrait)]; // 切换为竖屏
        self.playerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH * 0.55);
    } else {
        [RotationScreen forceOrientation:(UIInterfaceOrientationLandscapeRight)]; // 否则，切换为横屏
    }
    
}

@end
